package com.dk.util.wx;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaoleilu.hutool.http.HttpRequest;
import com.xiaoleilu.hutool.http.HttpUtil;

public class WxUtil {
	public static String AppID="wx25682e2c2e7209aa";
	public static String AppSecret="e9370b9d8fcd39abe1f109eb35313e3b";
	public static String access_token="";

	public static String token() {
		String res =  HttpUtil.get("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+AppID+"&secret="+AppSecret);
		System.out.println(res);
		JSONObject object = JSON.parseObject(res);
		access_token=object.getString("access_token");
		return access_token;
	}
	
	public static String 设置菜单() {
		JSONObject body=new JSONObject();
		JSONArray button=new JSONArray();

		button.add(createButton("view", "首页", "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+AppID+"&redirect_uri=https://fengzhihao.xyz/dk&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"));
		button.add(createButton("view", "套现", "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+AppID+"&redirect_uri=https://fengzhihao.xyz/dk/edtx.html&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"));
		button.add(createButton("view", "管理", "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+AppID+"&redirect_uri=https://fengzhihao.xyz/dk/adminIndex.html&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect"));
//		button.add(createButton("view", "测试", "http://a4399518sqa.6655.la/dk1/wx?echostr=aaa"));
		
		body.put("button", button);
		return HttpRequest.post("https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+access_token)
				.body(body.toJSONString()).execute().body();
		
	}
	private static JSONObject createButton(String type,String name,String url){
		JSONObject object=new JSONObject();
		object.put("type", type);
		object.put("name", name);
		object.put("url", url);
		return object;
	}
	public static JSONObject htmlAccessToken(String code) {
		String res =  HttpUtil.get("https://api.weixin.qq.com/sns/oauth2/access_token?appid="+AppID+"&secret="+AppSecret+"&code="+code+"&grant_type=authorization_code");
		return JSON.parseObject(res);
	}
	public static JSONObject getUserInfo(JSONObject htmlAccessToken) {
		String res =  HttpUtil.get("https://api.weixin.qq.com/sns/userinfo?access_token="+htmlAccessToken.getString("access_token")+"&openid="+htmlAccessToken.getString("openid")+"&lang=zh_CN");
		return JSON.parseObject(res);
	}
	public static void main(String[] args) {
//		System.out.println(JSON.toJSONString(getUserInfo(htmlAccessToken("001hKY3q0vHeTr1Be96q0zoQ3q0hKY3n")), true));
		System.out.println(token());
		System.out.println(设置菜单());
	}
}
